from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker,session


#SQLALCHEMY_DATABASE_URL='sqllite:///./DataBase.sqlite3'
engin=create_engine(
    'mysql+pymysql://root:123456@localhost/test1?charset=utf8mb4',  #mysql的编辑
    echo=True,
    #任意线程都可以使用
    #connect_args={'check_same_thread':False}
)

#创建数据库的会话
Session = sessionmaker(bind=engin, autoflush=False, autocommit=False, expire_on_commit=True)
Base=declarative_base(bind=engin,name='Base')
def getSession():
    opt:session.Session=Session()
    try:
        return opt
    except:
        Exception('数据库获取失败')
    finally:
        opt.close()